package com.gzhryc.shared_device.common.payment.services;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.common.payment.dao.WXPayAbnormalPayOrderDao;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayOrder;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayAbnormalPayOrder;
import com.gzhryc.shared_device.common.payment.services.dto.WXPayAbnormalPayOrderSearch;

import java.sql.SQLException;
import java.util.List;

public class WXPayAbnormalPayOrderService {

    static Logger log = Logger.getLogger(WXPayAbnormalPayOrderService.class);

    public static WXPayAbnormalPayOrderService self() {
        return MultiDBTools.getService(WXPayAbnormalPayOrderService.class, true);
    }

    public static WXPayAbnormalPayOrderService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, WXPayAbnormalPayOrderService.class, true);
    }

    WXPayAbnormalPayOrderDao dao;

    public WXPayAbnormalPayOrderService(String jdbcKey) {
        dao = new WXPayAbnormalPayOrderDao(jdbcKey);
    }

    public List<WXPayAbnormalPayOrder> findBySearch(WXPayAbnormalPayOrderSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        conditions.desc("createDate");

        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(WXPayAbnormalPayOrderSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public WXPayAbnormalPayOrder getByTradeNo(String tradeNo){
        try {
            return dao.getById(tradeNo);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(WXPayAbnormalPayOrder timeoutPayOrder){
        //检查一下是否存在
        WXPayOrder payOrder = WXPayOrderService.self(timeoutPayOrder.getCreateDate()).getByTradeNo(timeoutPayOrder.getTradeNo());
        if(payOrder != null) {
            try {
                return dao.insert(timeoutPayOrder) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    /**
     * 检查微信支付订单，存在则删除
     * @param tradeNo
     * @return
     * @throws LogicException
     */
    public boolean checkToDelete(String tradeNo) throws LogicException{
        WXPayAbnormalPayOrder payTimeoutPayOrder = getByTradeNo(tradeNo);
        return checkToDelete(payTimeoutPayOrder);
    }

    public boolean checkToDelete( WXPayAbnormalPayOrder payTimeoutPayOrder) throws LogicException {
        if (payTimeoutPayOrder != null) {
            WXPayOrder wxPayOrder = WXPayOrderService.self(payTimeoutPayOrder.getCreateDate()).getByTradeNo(payTimeoutPayOrder.getTradeNo());
            if (wxPayOrder != null) {
                try {
                    return dao.deleteById(payTimeoutPayOrder.getTradeNo()) > 0;
                } catch (SQLException e) {
                    log.error(e.getMessage(), e);
                }
            } else {
               throw  new LogicException("支付订单不存在，请确定后直接删除");
            }
        }
        return false;
    }

    public boolean deleteById(String tradeNo) throws LogicException {
        try {
            return dao.deleteById(tradeNo) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }
}
